package com.googlecode.bpmcenter.controller;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;

import com.googlecode.bpmcenter.service.ProcessService;

@ManagedBean(name = "processController")
public class ProcessController implements Serializable {

	private static final long serialVersionUID = 1L;

	@ManagedProperty(value = "#{processService}")
	ProcessService processService;
	
	public List<ProcessDefinition> getProcessDefinitionList() {
		return getProcessService().getProcessDefinitionList();
	}

	public String startProcessInstance(String key) {
		ProcessInstance processInstance = getProcessService().startProcessInstanceByKey(key);
		String id = processInstance.getId();
		String mensagem = "Processo " + id + " iniciado com sucesso!";
		FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Aviso", mensagem);
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		return "/ui/home";
	}
	
	public String completeTask(String taskId){
		getProcessService().completeTask(taskId);
		
		String mensagem = "Tarefa " + taskId + " completada com sucesso!";
		FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Aviso", mensagem);
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		return "/ui/home";
	}
	
	public ProcessService getProcessService() {
		return processService;
	}

	public void setProcessService(ProcessService processService) {
		this.processService = processService;
	}

}